using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class GifManager
	{
		#region CONSTRUCTORS
		static GifManager()
		{
			_data = new GifData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static GifData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static DataTable Insert( string gifName,  int categoryID,  string uRL1,  string uRL2,  string uRL3,  string uRL4,  string uRL5,  string location,  string mime,  long sMSCount,  DateTime createdDate,  string createdBy,  byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[13];
				values[0] = Util.ReplaceNullTo(gifName, DBNull.Value);
				values[1] = categoryID;
				values[2] = Util.ReplaceNullTo(uRL1, DBNull.Value);
				values[3] = Util.ReplaceNullTo(uRL2, DBNull.Value);
				values[4] = Util.ReplaceNullTo(uRL3, DBNull.Value);
				values[5] = Util.ReplaceNullTo(uRL4, DBNull.Value);
				values[6] = Util.ReplaceNullTo(uRL5, DBNull.Value);
				values[7] = Util.ReplaceNullTo(location, DBNull.Value);
				values[8] = Util.ReplaceNullTo(mime, DBNull.Value);
				values[9] = Util.ReplaceNullTo(sMSCount, DBNull.Value);
				values[10] = Util.ReplaceNullTo(createdDate, DBNull.Value);
				values[11] = Util.ReplaceNullTo(createdBy, DBNull.Value);
				values[12] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable Insert(GifInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[13];
				values[0] = Util.ReplaceNullTo(info.GifName, DBNull.Value);
				values[1] = info.CategoryID;
				values[2] = Util.ReplaceNullTo(info.URL1, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.URL2, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.URL3, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.URL4, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.URL5, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.Location, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.Mime, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.SMSCount, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
				values[11] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
				values[12] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static bool Update( long gifID,  string gifName,  int categoryID,  string uRL1,  string uRL2,  string uRL3,  string uRL4,  string uRL5,  string location,  string mime, byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[11];
				values[0] = gifID;
				values[1] = Util.ReplaceNullTo(gifName, DBNull.Value);
				values[2] = categoryID;
				values[3] = Util.ReplaceNullTo(uRL1, DBNull.Value);
				values[4] = Util.ReplaceNullTo(uRL2, DBNull.Value);
				values[5] = Util.ReplaceNullTo(uRL3, DBNull.Value);
				values[6] = Util.ReplaceNullTo(uRL4, DBNull.Value);
				values[7] = Util.ReplaceNullTo(uRL5, DBNull.Value);
				values[8] = Util.ReplaceNullTo(location, DBNull.Value);
				values[9] = Util.ReplaceNullTo(mime, DBNull.Value);
				values[10] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( GifInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[11];
				values[0] = info.GifID;
				values[1] = Util.ReplaceNullTo(info.GifName, DBNull.Value);
				values[2] = info.CategoryID;
				values[3] = Util.ReplaceNullTo(info.URL1, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.URL2, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.URL3, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.URL4, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.URL5, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.Location, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.Mime, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( long gifID)
		{
			_error = "";
			try
			{
				object[] values = { gifID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( GifInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.GifID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static GifInfo Select( long gifID)
		{
			_error = "";
			try
			{
				object[] values = { gifID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				GifInfo info = new GifInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable SelectByCate(int categoryID, int pageIndex, int pageSize)
        {
            _error = "";
            try
            {
                object[] values = new object[1];
                values[0] = Util.ReplaceNullTo(categoryID, DBNull.Value);

                int startIndex = pageIndex * pageSize;
                return _data.SelectByCate(values, startIndex, pageIndex);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SearchBy(int intCateId, string strName, DateTime fromDate, DateTime toDate, int pageIndex, int pageSize)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = intCateId;
                values[1] = strName;
                values[2] = fromDate;
                values[3] = toDate;

                int startIndex = pageIndex * pageSize;
                return _data.SearchBy(values, startIndex, pageIndex);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return GifManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return GifManager.Search( "", "", pageIndex, pageSize);
		}
		
		#endregion
		
	}
}